package servlet;

import dao.UserDAO1;
import entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "userUpdate", value = "/user/update")
public class PersonUpdateServlet extends HttpServlet{

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String id = req.getParameter("id");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String email = req.getParameter("email");
        String mobile = req.getParameter("mobile");
        String address = req.getParameter("address");
        String status = req.getParameter("status");
        String age = req.getParameter("age");
        String imgUrl = req.getParameter("imageUrl");

        if(status == null) status = "1";

        User user = new User();
        user.setAddress(address);
        user.setAge(Integer.parseInt(age));
        user.setEmail(email);
        user.setId(id);
        user.setMobile(mobile);
        user.setUsername(username);
        user.setPassword(password);
        user.setStatus(Integer.parseInt(status));
        user.setImgUrl(imgUrl);
        UserDAO1 userDAO1 = new UserDAO1();
        userDAO1.updatePerson(user);
        System.out.println(user);
        req.getRequestDispatcher("/admin/findByName").forward(req, resp);
    }
}
